package pe.edu.claretiano.admisionmatricula.matricula.daoimpl;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;

import pe.edu.claretiano.admisionmatricula.bean.DetalleHorario;
import pe.edu.claretiano.admisionmatricula.bean.Horario;
import pe.edu.claretiano.admisionmatricula.bean.Matricula;
import pe.edu.claretiano.admisionmatricula.factory.MyBatisConnectionFactory;
import pe.edu.claretiano.admisionmatricula.matricula.dao.HorarioDao;

public class HorarioDaoImpl implements HorarioDao {
	
	private final static Logger logHorarioDaoImpl = Logger.getLogger(HorarioDaoImpl.class);

	private SqlSessionFactory sqlSessionFactory;

	public HorarioDaoImpl() {
		sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
	}

	@Override
	public Matricula obtenerMatriculaRegistrada(Matricula matricula) {
		SqlSession session = sqlSessionFactory.openSession();
		Matricula matriculaHorario = null;
		try {
			matriculaHorario = (Matricula)session.selectOne("Horario.obtenerMatriculaHorario",matricula);
		} catch (Exception ex) {
			logHorarioDaoImpl.error(ex.getMessage());
		} finally {
			session.close();
		}
		
		return matriculaHorario;
	}

	@Override
	public Horario obtenerHorario(Horario horario) {
		SqlSession session = sqlSessionFactory.openSession();
		Horario bean = null;
		try {
			bean = (Horario)session.selectOne("Horario.obtenerHorario",horario);
		} catch (Exception ex) {
			logHorarioDaoImpl.error(ex.getMessage());
		} finally {
			session.close();
		}
		return bean;
	}

	@Override
	public List<DetalleHorario> obtenerDetalleHorario(Horario horario) {
		SqlSession session = sqlSessionFactory.openSession();
		List<DetalleHorario> lstDetalleHorario = new ArrayList<DetalleHorario>();
		
		try {
			lstDetalleHorario = (List<DetalleHorario>) session.selectList("Horario.obtenerDetalleHorario", horario);
		} catch (Exception ex) {
			logHorarioDaoImpl.error(ex.getMessage());
		} finally {
			session.close();
		}
		return lstDetalleHorario;
	}

	@Override
	public void actualizarHorarioMatricula(Matricula matricula) {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			session.update("Horario.updateHorarioMatricula",matricula);
			session.commit();
		} catch (Exception ex) {
			logHorarioDaoImpl.error(ex.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
		
	}

	@Override
	public Matricula obtenerHorarioRegistrado(Matricula matricula) {
		SqlSession session = sqlSessionFactory.openSession();
		Matricula matriculaHorario = null;
		try {
			matriculaHorario = (Matricula)session.selectOne("Horario.obtenerHorarioRegistrado",matricula);
		} catch (Exception ex) {
			logHorarioDaoImpl.error(ex.getMessage());
		} finally {
			session.close();
		}
		
		return matriculaHorario;
	}


}
